package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.client.dto.cust.ContextCustDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.order.IOrderService;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttrDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.cucc.service.util.KiteDcPublicUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CustInfoNameHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private IOrderService orderService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        ContextCustDTO custdTO = KiteWebContext.getContext().getCust();
        CuccContextCustDTO custDTO = KiteBeanUtils.transform(custdTO, CuccContextCustDTO.class);
        String taskCode = querySceneInstService.getExtParameterValue(sceneInstId, "taskCode");
        if (KiteStringUtils.equalsAny(taskCode, "OrderResubmit", "OrderSignedCheckSHZWY", "OrderResubmitZWY")
            && KiteStringUtils.equals(ServiceOfferConsts.SERVICE_OFFER_TRANSFER, sceneInst.getServiceOfferId())) {
            String orderId = this.querySceneInstService
                .getExtParameterValue(sceneInstId, "outOrderId");
            Map<String, Object> orderInfo = orderService.getOrderDetail(orderId);
            KitePosDTO kitePosDTO = JSONObject
                .toJavaObject(JSON.parseObject(JSON.toJSONString(orderInfo)), KitePosDTO.class);
            List<KitePosAttrDTO> attrs = kitePosDTO.getCustomer().getAttrs();
            String oldUserName = "";
            if (KiteListUtils.isNotEmpty(attrs)) {
                for (KitePosAttrDTO attr : attrs) {
                    if (KiteStringUtils.equals("oldUserName", attr.getCode())) {
                        oldUserName = attr.getValue();
                    }
                }
                commonAttr.setValue(oldUserName);
                commonAttr.setValueDesc(oldUserName);
                setCommonAttr(commonAttr);
            }
        }
        if (KiteStringUtils.isEmpty(commonAttr.getValue())) {
            StringBuilder custName = new StringBuilder(custDTO.getCustName());
            if (KiteDcPublicUtils.isCollectionGroup(sceneInstId) && KiteStringUtils.equals("group_user_name", commonAttr.getFieldName())) {
                custName.append("收入归集集团");
            }
            commonAttr.setValue(custName.toString());
            commonAttr.setValueDesc(custName.toString());
            setCommonAttr(commonAttr);
        }
    }

}
